package minisoft.application.loterryservice;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.content.Context;

public class TodayDataSource {

	public List<Today> searchTodaysByDateInWeek(Context context, int dateOpen) {
		List<Today> todays = new ArrayList<Today>();

		String[] array_today = null;

		switch (dateOpen) {
		case Calendar.SUNDAY: {
			array_today = context.getResources().getStringArray(
					R.array.schedule_sunday);
			break;
		}
		case Calendar.MONDAY: {
			array_today = context.getResources().getStringArray(
					R.array.schedule_monday);
			break;
		}
		case Calendar.TUESDAY: {
			array_today = context.getResources().getStringArray(
					R.array.schedule_tuesday);
			break;
		}
		case Calendar.WEDNESDAY: {
			array_today = context.getResources().getStringArray(
					R.array.schedule_wednesday);
			break;
		}
		case Calendar.THURSDAY: {
			array_today = context.getResources().getStringArray(
					R.array.schedule_thursday);
			break;
		}
		case Calendar.FRIDAY: {
			array_today = context.getResources().getStringArray(
					R.array.schedule_friday);
			break;
		}
		case Calendar.SATURDAY: {
			array_today = context.getResources().getStringArray(
					R.array.schedule_saturday);
			break;
		}
		default: // bla
		}

		String[] array_region = context.getResources().getStringArray(
				R.array.region_arrays);
		String[] array_north = context.getResources().getStringArray(
				R.array.region_north);
		String[] array_middle = context.getResources().getStringArray(
				R.array.region_middle);
		String[] array_south = context.getResources().getStringArray(
				R.array.region_south);

		for (int i = 0; i < array_today.length; ++i) {
			String timeSpan = "";
			String region = "";
			String provinceName = "";
			int regionId = 0;
			provinceName = array_today[i];
			boolean flag = false;
			if (!flag) {
				for (int i1 = 0; i1 < array_north.length; ++i1) {
					if (array_north[i1].equals(provinceName)) {
						region = array_region[1];
						regionId = 1;
						timeSpan = context.getResources().getString(
								R.string.Time_north);
						flag = true;
						break;
					}
				}
			}

			// middle
			if (!flag) {
				for (int i1 = 0; i1 < array_middle.length; ++i1) {
					if (array_middle[i1].equals(provinceName)) {
						region = array_region[2];
						regionId = 2;
						timeSpan = context.getResources().getString(
								R.string.Time_middle);
						flag = true;
						break;
					}
				}
			}
			if (!flag) {
				for (int i1 = 0; i1 < array_south.length; ++i1) {
					if (array_south[i1].equals(provinceName)) {
						region = array_region[3];
						regionId = 3;
						timeSpan = context.getResources().getString(
								R.string.Time_south);
						flag = true;
						break;
					}
				}
			}

			// add to today
			Today today = new Today(provinceName, region, regionId, timeSpan);
			todays.add(today);
		}

		return todays;
	}

	public List<Today> searchTodaysByDateInWeekAndRegion(Context context,
			int dateOpen, int regionId) {
		List<Today> todays = new ArrayList<Today>();

		String[] array_today = null;

		switch (dateOpen) {
		case Calendar.SUNDAY: {
			array_today = context.getResources().getStringArray(
					R.array.schedule_sunday);
			break;
		}
		case Calendar.MONDAY: {
			array_today = context.getResources().getStringArray(
					R.array.schedule_monday);
			break;
		}
		case Calendar.TUESDAY: {
			array_today = context.getResources().getStringArray(
					R.array.schedule_tuesday);
			break;
		}
		case Calendar.WEDNESDAY: {
			array_today = context.getResources().getStringArray(
					R.array.schedule_wednesday);
			break;
		}
		case Calendar.THURSDAY: {
			array_today = context.getResources().getStringArray(
					R.array.schedule_thursday);
			break;
		}
		case Calendar.FRIDAY: {
			array_today = context.getResources().getStringArray(
					R.array.schedule_friday);
			break;
		}
		case Calendar.SATURDAY: {
			array_today = context.getResources().getStringArray(
					R.array.schedule_saturday);
			break;
		}
		default: // bla
		}

		String[] array_region = context.getResources().getStringArray(
				R.array.region_arrays);
		String[] array_province = null;
		String timeSpan = "";
		switch (regionId) {
		case 1:
			array_province = context.getResources().getStringArray(
					R.array.region_north);
			timeSpan = context.getResources().getString(
					R.string.Time_north);
			break;
		case 2:
			array_province = context.getResources().getStringArray(
					R.array.region_middle);
			timeSpan = context.getResources().getString(
					R.string.Time_middle);
			break;
		case 3:
			array_province = context.getResources().getStringArray(
					R.array.region_south);
			timeSpan = context.getResources().getString(
					R.string.Time_south);
			break;

		}

		for (int i = 0; i < array_today.length; ++i) {
			
			String region = "";
			String provinceName = "";
			provinceName = array_today[i];
			boolean flag = false;
			for (int i1 = 0; i1 < array_province.length; ++i1) {
				if (array_province[i1].equals(provinceName)) {
					region = array_region[1];
					
					flag = true;
					break;
				}
			}

			// add to today
			if (flag) {
				Today today = new Today(provinceName, region, regionId,
						timeSpan);
				todays.add(today);
			}
		}

		return todays;
	}

	/*
	 * // Database fields private SQLiteDatabase database; private
	 * MySQLiteHelper dbHelper; private String[] todayColumns = {
	 * MySQLiteHelper.TODAY_ID, MySQLiteHelper.TODAY_PROVINCE_NAME,
	 * MySQLiteHelper.TODAY_DATEOPEN, MySQLiteHelper.TODAY_REGION,
	 * MySQLiteHelper.TODAY_TIMESPAN };
	 * 
	 * 
	 * public TodayDataSource() {
	 * 
	 * }
	 * 
	 * public TodayDataSource(Context context) { dbHelper = new
	 * MySQLiteHelper(context); }
	 * 
	 * public void open() throws SQLException { database =
	 * dbHelper.getWritableDatabase(); }
	 * 
	 * public void close() { dbHelper.close(); }
	 * 
	 * public Today createToday(String todayProvinceName, int dateOpen, String
	 * region, String timeSpan) { ContentValues values = new ContentValues();
	 * values.put(MySQLiteHelper.TODAY_PROVINCE_NAME, todayProvinceName);
	 * values.put(MySQLiteHelper.TODAY_DATEOPEN, dateOpen);
	 * values.put(MySQLiteHelper.TODAY_REGION, region);
	 * values.put(MySQLiteHelper.TODAY_TIMESPAN, timeSpan);
	 * 
	 * int insertId = (int) database.insert(MySQLiteHelper.TABLE_TODAY, null,
	 * values);
	 * 
	 * Cursor cursor = database.query(MySQLiteHelper.TABLE_TODAY, todayColumns,
	 * MySQLiteHelper.TODAY_ID + " = " + insertId, null, null, null, null);
	 * 
	 * Today newToday = new Today(); try { cursor.moveToFirst(); newToday =
	 * cursorToToday(cursor); } catch (Exception ex) { } finally { // Make sure
	 * to close the cursor cursor.close(); }
	 * 
	 * return newToday; }
	 * 
	 * public void deleteToday(Today today) { int id = today.getId();
	 * System.out.println("Today deleted with id: " + id);
	 * database.delete(MySQLiteHelper.TABLE_TODAY, MySQLiteHelper.TODAY_ID +
	 * " = " + id, null); }
	 * 
	 * public List<Today> searchTodays(int dateOpen) { List<Today> todays = new
	 * ArrayList<Today>();
	 * 
	 * Cursor cursor = database.query(MySQLiteHelper.TABLE_TODAY, todayColumns,
	 * MySQLiteHelper.TODAY_PROVINCE_NAME + " = " + dateOpen, null, null, null,
	 * null, null);
	 * 
	 * try { cursor.moveToFirst(); while (!cursor.isAfterLast()) { Today today =
	 * cursorToToday(cursor); todays.add(today); cursor.moveToNext(); } } catch
	 * (Exception ex) { } finally { // Make sure to close the cursor
	 * cursor.close(); }
	 * 
	 * return todays; }
	 * 
	 * private Today cursorToToday(Cursor cursor) { Today today = new Today();
	 * today.setId(cursor.getInt(0));
	 * today.setProvinceName(cursor.getString(1));
	 * today.setOpenDate(cursor.getInt(2));
	 * today.setRegion(cursor.getString(3));
	 * today.setTimeSpan(cursor.getString(4)); return today; }
	 */

}